# -*- coding:utf-8 -*-
from PySide6.QtWidgets import (QFileDialog)
from PySide6.QtCore import (QDir)

from app.model.model import AppModel
from app.view.view import AppView

from app.features.service.export_service import ExportService

class AppController:
    """
    mvc 框架 controller
    """
    def __init__(self, model:AppModel, view:AppView):
        self.model = model
        self.view = view
        self.view.bind_controller(self)
        # 连接single 和 solt
        self.model.excel_dir_changed.connect(self.view.update_excel_path)
        self.model.export_dir_changed.connect(self.view.update_export_path)
        self.model.delimiter_changed.connect(self.view.update_excel_path)
        self.model.logging_changed.connect(self.view.update_log_view)


    # 响应处理方法
    def handle_excel_directory_selection(self):
        """
        Excel文件目录按钮新号响应方法
        """
        directory = QFileDialog.getExistingDirectory(
            self.view,
            "选择Excel文件目录",
            QDir.homePath(),
            QFileDialog.ShowDirsOnly
        )
        if directory:
            self.model.excel_dir = directory

    def handle_export_directory_selection(self):
        """
        导出目录按钮信号响应方法
        """
        directory = QFileDialog.getExistingDirectory(
            self.view,
            "选择文件导出目录",
            QDir.homePath(),
            QFileDialog.ShowDirsOnly
        )
        if directory:
            self.model.export_dir = directory


    def handle_delimiter_editing_finished(self):
        """
        文件名分割符设置完成信号响应方法
        """
        self.model.delimiter = self.view.delimiter_group.delimiter_edit.text()


    def handle_export_type_cb_update(self):
        """
        选择导出类型
        """
        selected = [cb.text() for cb in self.view.export_type_group.export_type_cb if cb.isChecked()]
        self.model.export_type = ", ".join(selected)

    def handle_clear_log(self):        
        """
        清除日志
        """
        self.model.log_content = ""

    def handle_refresh(self):
        self.model.add_log("系统手动刷新")
        # 这里可以添加实际的刷新逻辑

    def handle_export(self):
        """
        导出操作
        """
        self.model.add_log("导出配置操作")
        export_service = ExportService(self)
        export_service.export()